﻿using System;
using System.Collections.Generic;
using System.Linq;
using CID.Domain.Exceptions;
using CID.Domain.Infrastructure;
using CID.Domain.Operations;
using CID.Domain.Users;
using NailsFramework.Persistence;

namespace CID.Domain.Development
{
    public class Component : Model<Component>, IComponent
    {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual bool Enabled { get; set; }
        public virtual IUser Responsible { get; set; }
        public virtual IActivationDeactivationDefinition ActivationDeactivationDefinition { get; private set; }
        public virtual IServerRole RequiredServerRole { get; set; } 

        private IList<IVersion> versions = new List<IVersion>();
        public virtual IEnumerable<IVersion> Versions { get { return versions; } }

        private IList<IVersion> dependants = new List<IVersion>();
        public virtual IEnumerable<IVersion> Dependants { get { return dependants; } }

        protected Component()
        {
            ActivationDeactivationDefinition = new ActivationDeactivationDefinition(this);
            Enabled = true;
        }

        public Component(string name, string description, IServerRole requiredServerRole, IUser responsible) : this()
        {
            Name = name;
            Description = description;
            RequiredServerRole = requiredServerRole;
            Responsible = responsible;
        }

        #region Equals and GetHashCode
        //This class needs to have the Equals overriden to support the comparison with the proxies from the persistence layer
        public override bool Equals(object obj)
        {
            return EntityEqualityHelper.Equals(this, obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion

        public virtual IVersion CreateVersion(string name, string description) 
        {
            var duplicateVersion = versions.FirstOrDefault(x=> x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
            if (duplicateVersion != null) throw new DuplicateChildException(this, duplicateVersion);
            var version = new Version(this, name, description);
            AddVersion(version);
            return version;
        }

        //TODO: this method should be removed or moved to private. This needs to change some tests
        public virtual void AddVersion(IVersion version)
        {
            if ((Equals(version.Component) && !versions.Contains(version)))
                versions.Add(version);
        }

        public virtual void RemoveVersion(IVersion version)
        {
            if (Equals(version.Component))
            {
                if ((version.Release != null) && (version.Release.Status == ReleaseStatus.Released)) 
                    throw new InvalidActionForReleaseStatusException("remove Version", ReleaseStatus.Released);
                versions.Remove(version);
            }
        }

        public virtual void AddDependant(IVersion version)
        {
            if (!dependants.Contains(version)) dependants.Add(version);
        }

        public virtual void RemoveDependant(IVersion version)
        {
            if (dependants.Contains(version)) dependants.Remove(version);
        }
    }
}
